local xiaowu = fk.CreateSkill{
  name = "hanqing__xiaowu",
}

Fk:loadTranslationTable{
  ["hanqing__xiaowu"] = "啸乌",
  [":hanqing__xiaowu"] = "出牌阶段，你可以横置或重置本轮第X名执行回合的角色并重铸X张牌（X为中央区牌数），若其因此被重置，重置〖灼翎〗。",

  ["#hanqing__xiaowu"] = "啸乌：重铸%arg张牌并横置或重置 %dest",
}

xiaowu:addEffect("active", {
  anim_type = "offensive",
  prompt = function (self, player)
    local n = #Fk:currentRoom():getBanner("@$CenterArea")
    return "#hanqing__xiaowu::"..player:getTableMark("hanqing__xiaowu-round")[n]..":"..n
  end,
  card_num = function ()
    return #Fk:currentRoom():getBanner("@$CenterArea")
  end,
  target_num = 0,
  can_use = function(self, player)
    local n = #Fk:currentRoom():getBanner("@$CenterArea")
    return n > 0 and #player:getTableMark("hanqing__xiaowu-round") >= n and
      not Fk:currentRoom():getPlayerById(player:getTableMark("hanqing__xiaowu-round")[n]).dead
  end,
  card_filter = function (self, player, to_select, selected)
    return #selected < #Fk:currentRoom():getBanner("@$CenterArea")
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = room:getPlayerById(player:getTableMark("hanqing__xiaowu-round")[#room:getBanner("@$CenterArea")])
    room:doIndicate(player, {target})
    room:recastCard(effect.cards, player, xiaowu.name)
    if not target.dead then
      if target.chained then
        if target:setChainState(false) then
          player:setSkillUseHistory("hanqing__zhuoling", 0, Player.HistoryPhase)
        end
      else
        target:setChainState(true)
      end
    end
  end,
})

xiaowu:addEffect(fk.TurnStart, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(xiaowu.name, true)
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:addTableMark(player, "hanqing__xiaowu-round", target.id)
  end,
})

xiaowu:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  room:addSkill("#CenterArea")
  if not is_start then
    local round_event = room.logic:getCurrentEvent():findParent(GameEvent.Round, true)
    if round_event then
      local record = {}
      room.logic:getEventsOfScope(GameEvent.Turn, 1, function (e)
        table.insert(record, e.data.who.id)
      end, Player.HistoryRound)
      room:setPlayerMark(player, "hanqing__xiaowu-round", record)
    end
  end
end)

return xiaowu
